package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 1/8/21 10:31 AM
 *
 *  https://leetcode-cn.com/problems/rotate-array/
 */
public class _189_旋转数组 {

    public void rotate(int[] nums, int k) {

        if (nums == null || nums.length <= 1 || k <= 0) {

            return;
        }

        int preIndex = 0;
        int index = 0;
        int num = nums[0];
        int count = 0;
        while (true) {

            index = (index + k) % nums.length;


            int tmp = nums[index];
            nums[index] = num;
            num = tmp;
            count++;

            if (index == preIndex) {

                if (count == nums.length) {

                    break;
                }else {

                    preIndex = (preIndex+1) % nums.length;
                    index = preIndex;
                    num = nums[index];
                }
            }
        }

    }

}
